package com.ark.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ark.yygh.hosp.repository.DepartmentRepository;
import com.ark.yygh.hosp.service.DepartmentService;
import com.ark.yygh.model.hosp.Department;
import com.ark.yygh.vo.hosp.DepartmentQueryVo;
import com.ark.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室
    @Override
    public void save(Map<String, Object> switchMap) {
        Department department = JSONObject
                .parseObject(JSONObject.toJSONString(switchMap), Department.class);
        //根据医院编号和科室编号进行查询
        Department departmentBy =
                departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        //判断
        if (departmentBy==null){
            //不存在则新增
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        } else {
            //存在则更新
            departmentBy.setUpdateTime(new Date());
            departmentBy.setIsDeleted(0);
            departmentRepository.save(departmentBy);
        }
    }

    /**
     * 查询科室列表
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //对象转换
        Department department = new Department();
        department.setIsDeleted(0);
        BeanUtils.copyProperties(departmentQueryVo,department);

        //分页
        Pageable pageable = PageRequest.of(page-1,limit);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department, matcher);

        //调用分页方法
        Page<Department> departmentPage = departmentRepository.findAll(example,pageable);
        return departmentPage;
    }

    //删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if (department!=null){
            departmentRepository.delete(department);
        }
    }

    //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //定义最终返回结果集合
        List<DepartmentVo> resultList = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department dept = new Department();
        dept.setHoscode(hoscode);
        Example<Department> example = Example.of(dept);
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> map =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map
        for (Map.Entry<String, List<Department>> entry : map.entrySet()) {
            //大科室编号
            String code = entry.getKey();
            //大科室对应数据
            List<Department> list = entry.getValue();

            //封装大科室
            DepartmentVo departVo1 = new DepartmentVo();
            departVo1.setDepcode(code);
            departVo1.setDepname(list.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> departVoList = new ArrayList<>();
            for (Department depart : list) {
                DepartmentVo departVo2 = new DepartmentVo();
                departVo2.setDepcode(depart.getDepcode());
                departVo2.setDepname(depart.getDepname());
                departVoList.add(departVo2);
            }

            //把小科室放到大科室里
            departVo1.setChildren(departVoList);
            //再将大科室放到最终集合
            resultList.add(departVo1);
        }

        return resultList;
    }

    //根据医院编号，科室编号获取科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null){
            return department.getDepname();
        }
        return "";
    }
}
